﻿using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace log4net
{
    public class MeLogService
    {
        public MeLogService()
        {
            Init();
        }
        System.Collections.Concurrent.ConcurrentQueue<MyStruct> queue = null;
        StreamWriter sw = null;
        public void Init()
        {
            queue = new System.Collections.Concurrent.ConcurrentQueue<MyStruct>();
            var path = AppDomain.CurrentDomain.BaseDirectory + "log\\";
            var ext = Directory.Exists(path);
            if (!ext)
            {
                Directory.CreateDirectory(path);
            }
            var file = path + DateTime.Now.ToString("yyyy-MM-dd");
            sw = File.CreateText(file);
            //dir.
            CancellationToken token = new CancellationToken();
            AppDomain.CurrentDomain.DomainUnload += (ao, ae) =>
            {

            };
            Task task = new Task(() =>
            {

            }, token, TaskCreationOptions.LongRunning);
           
        }

        public void AddLog(LogLevel level, string msg, Exception ex)
        {
            queue.Enqueue(new MyStruct()
            {
                ex = ex,
                level = level,
                msg = msg
            });
        }

        static void WriteLog(LogLevel level, string msg = "", Exception ex = null)
        {
            if (log == null)
            {
                return;
            }

            switch (level)
            {
                case LogLevel.DeBug:
                    log.Debug(msg, ex);
                    break;
                case LogLevel.Info:
                    log.Info(msg, ex);
                    break;
                case LogLevel.Warning:
                    log.Warn(msg, ex);
                    break;
                case LogLevel.Exception:
                    log.Error(msg, ex);
                    break;
                default:
                    break;
            }
        }
    }
    struct MyStruct
    {
        public LogLevel level { get; set; }
        public string msg { get; set; }
        public Exception ex { get; set; }
    }
}
